package org.tigr.htc.cmd;

/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import java.io.StringReader;

import org.tigr.antware.shared.testing.TIGRTestCase;
import org.tigr.antware.shared.util.Logger;

/**
 * The class <code>CommandHelperTest</code> models
 */

public class CommandHelperTest extends TIGRTestCase {
    /**
     * The variable <code>logger</code> holds the instance of the logger for this class.
     */
    private static Logger logger = new Logger("org.tigr.htc.cmd.CommandHelperTest");
    
    /**
     * The constructor <code>CommandHelperTest</code> creates a new instance
     * 
     * @param name
     */
    public CommandHelperTest(String name) {
        super(name);
    }
    
    public void testMarshalTaskGroup () {
        TaskGroup tg = new TaskGroup ();
        tg.setCmdId(2);
        tg.put(new Integer(1), new Task(1, CmdState.FINISHED.toString(), 0, ""));
        tg.put(new Integer(2), new Task(2, CmdState.FAILED.toString(), -1, "Failed task."));
        tg.put(new Integer(3), new Task(3, CmdState.INTERRUPTED.toString(), 0, "Interrupted task"));
        
        // Marshal this object
        String tgString = CommandHelper.getInstance().marshal(tg);
        if (logger.isDebugEnabled()) {
            logger.debug("XML representation: " + tgString);
        }
        String stdString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
                    "<taskGroup><htcID>2</htcID>" + 
                    "<task><index>3</index><state>INTERRUPTED</state><returnValue>0</returnValue><message>Interrupted task</message></task>" + 
                    "<task><index>2</index><state>FAILED</state><returnValue>-1</returnValue><message>Failed task.</message></task>" + 
                    "<task><index>1</index><state>FINISHED</state><returnValue>0</returnValue><message></message></task></taskGroup>";
        assertTrue("Expected:\n" + stdString + " \ngot:\n" + tgString, tgString.equals(stdString));
    }

    
    public void testUnmarshalTaskGroup () throws Exception {
        TaskGroup stdTG = new TaskGroup ();
        stdTG.setCmdId(2);
        stdTG.put(new Integer(1), new Task(1, CmdState.FINISHED.toString(), 0, ""));
        stdTG.put(new Integer(2), new Task(2, CmdState.FAILED.toString(), -1, "Failed task."));
        stdTG.put(new Integer(3), new Task(3, CmdState.INTERRUPTED.toString(), 0, "Interrupted task"));
        
        // Marshal this object
        String tgString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
                    "<taskGroup><htcID>2</htcID>" + 
                    "<task><index>3</index><state>INTERRUPTED</state><returnValue>0</returnValue><message>Interrupted task</message></task>" + 
                    "<task><index>2</index><state>FAILED</state><returnValue>-1</returnValue><message>Failed task.</message></task>" + 
                    "<task><index>1</index><state>FINISHED</state><returnValue>0</returnValue><message></message></task>" + 
                    "</taskGroup>";

        if (logger.isDebugEnabled()) {
            logger.debug("Unmarshalling string: \n" + tgString);
        }
        TaskGroup tg = (TaskGroup) CommandHelper.getInstance().unmarshal(new StringReader(tgString));
        if (logger.isDebugEnabled()) {
            logger.debug("Finished unmarshalling.");
        }
        if (logger.isDebugEnabled()) {
            logger.debug(tg);
        }
    }
    
    public static void main(String[] args) throws ClassNotFoundException {
        String className = "org.tigr.htc.cmd.CommandHelperTest";
        TIGRTestCase.testedClass = Class.forName(className);
        Logger.initializeLogging();
        
        // Parse the arguments and set the log level using the value if specified
        // otherwise it is set to 0
        TIGRTestCase.parseArguments(className, args);

        // Call the method in the parent class with the required parameters
        TIGRTestCase.runTestsFromCommandLine(className, args);
    } // end of main()
}
